SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE FUNCTION [Live].[GetInstrumentName] 
(
	-- Add the parameters for the function here
	@ExportId INT,
	@AssetIntrnlId INT 
)
RETURNS VARCHAR(150)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @ReturnVal VARCHAR(150)

	-- Add the T-SQL statements to compute the return value here
	SELECT 
		@ReturnVal = CASE WHEN a.AssetType <> 'Instrument' THEN i.InstrName + ' - ' + REPLACE(a.AssetType , 'DelayedDraw', 'Delayed Draw')
			ELSE i.InstrName
			END 
	
	
	 FROM  FSFundModel.Reporting.Asset a 
	INNER JOIN  FSFundModel.Reporting.Instrument i 
	ON a.InstrumentId =i.InstrumentId 
	AND i.ExportId = a.ExportId 



	WHERE a.ExportId = @ExportId
	AND a.AssetIntrnlId = @AssetIntrnlId

	-- Return the result of the function
	RETURN @ReturnVal

END
GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Live', 'SCHEMA', N'Live', 'FUNCTION', N'GetInstrumentName', NULL, NULL
GO
